package main

import "fmt"

//28ms 82%,8MB 79%
func findRepeatNumber(nums []int) int {
	numsMap := make(map[int]bool, 5)
	for _, v := range nums {
		if !numsMap[v] {
			numsMap[v] = true
		} else {
			return v
		}
	}
	return 0
}

//原地交换,48ms 5%,8.5MB 66%
func findRepeatNumber1(nums []int) int {
	for i := 0; i < len(nums); {
		if nums[i] == i {
			i++
			continue
		}
		tmp := nums[i]
		if nums[tmp] == tmp {
			return tmp
		} else {
			nums[i], nums[tmp] = nums[tmp], nums[i]
		}
	}
	return -1
}

func main() {
	fmt.Println(findRepeatNumber([]int{1, 1}))
	fmt.Println(findRepeatNumber1([]int{1, 2, 3, 1}))

}
